#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
	/*或者改用两个文件操作符来实现*/
	fstream filename ("./tmp", ios::out | ios::in | ios_base::trunc);
	const int LEN = 100;
	
	///*
	for ( int i = 0; i < LEN; i++ )
	{
		filename << i << endl;
	}
	
	//filename.flush();
	//*/
	
	/*&
	//测试代码
	int length = filename.tellg();
	cout << length << endl;
	int a;
	filename >> a;
	cout << a << endl;
	cout << " EOF is " << filename.eof() << endl;
	filename.seekg (0, ios::beg);
	
	cout << length - filename.tellg() << endl;
	cout << " EOF is " << filename.eof() << endl;
	*/
	
	//这两步很重要
	filename.seekg (0, ios::beg);
	//filename.clear();
	
	int a = 0;
	char buf[100];
	for ( int i = 0; i < LEN; i++ )
	{
		filename >> a;
		cout << a << endl;
	}
	cout << filename.tellg() << endl;
	return 0;
}
